package untils

import (
	"github.com/hashicorp/consul/api"
)

type ConfigClient struct {
	Address *api.Client
}

func NewConfigClient(address string) (*ConfigClient, error) {
	config := api.DefaultConfig()
	config.Address = address
	client, err := api.NewClient(config)
	if err != nil {
		return nil, err
	}
	return &ConfigClient{Address: client}, err
}

type Consul struct {
	ID      string
	Name    string
	Tags    []string
	Port    int
	Address string
}

func (c *ConfigClient) RegisterService(consul Consul) {
	registration := api.AgentServiceRegistration{
		ID:      consul.ID,
		Name:    consul.Name,
		Tags:    consul.Tags,
		Port:    consul.Port,
		Address: consul.Address,
	}
	err := c.Address.Agent().ServiceRegister(&registration)
	if err != nil {
		return
	}
}
